package org.abratuhi.kvngroupware.chat.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.KVNGroupwareClientAction;
import org.abratuhi.kvngroupware.chat.manager.ChatroomManager;
import org.abratuhi.kvngroupware.chat.model.Chatroom;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.widget.AbstractWorkWidget;
import org.jdom.Element;

@SuppressWarnings("serial")
public class GuiChatroom extends AbstractWorkWidget{
	LoginManager loginmanager;
	ChatroomManager chatroommanager;
	
	public JList listUsers = new JList(new String[0]);
	public JTextArea textareaMessages = new JTextArea();
	public JTextArea textareaMessage = new JTextArea();
	public JButton buttonSend = new JButton();
	
	public GuiChatroom(String frameid, String frametitle, MessageSender sender, Properties locale, LoginManager loginmanager, ChatroomManager chatroommanager){
		super(frameid, frametitle, sender, locale);
		
		this.loginmanager = loginmanager;
		this.chatroommanager = chatroommanager;
		
		buttonSend.setAction(new SendAction(locale.getProperty("chatroom.button.message.send"), null, null, null));
		
		listUsers.setPreferredSize(new Dimension(100, 200));

		textareaMessages.setRows(40);
		textareaMessages.setColumns(40);
		textareaMessages.setWrapStyleWord(true);
		textareaMessages.setLineWrap(true);

		textareaMessage.setRows(3);
		textareaMessage.setColumns(40);
		textareaMessage.setWrapStyleWord(true);
		textareaMessage.setLineWrap(true);

		JPanel users = new JPanel(new SpringLayout());
		users.setBorder(new TitledBorder(locale.getProperty("chatroom.border.users")));
		users.add(new JScrollPane(listUsers));
		SpringUtilities.makeCompactGrid(users, 1, 1, 0, 0, 0, 0);

		JPanel send = new JPanel(new SpringLayout());
		send.add(new JScrollPane(textareaMessage));
		send.add(buttonSend);
		SpringUtilities.makeCompactGrid(send, 1, 2, 0, 0, 0, 0);

		JPanel chat = new JPanel(new SpringLayout());
		chat.setBorder(new TitledBorder(locale.getProperty("chatroom.border.chat")));
		chat.add(new JScrollPane(textareaMessages));
		chat.add(send);
		SpringUtilities.makeCompactGrid(chat, 2, 1, 0, 0, 0, 0);

		setLayout(new SpringLayout());
		setVisible(true);
		setPreferredSize(new Dimension(400, 400));
		add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, users, chat));
		SpringUtilities.makeCompactGrid(this, 1, 1, 0, 0, 0, 0);
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		Chatroom chatroom = chatroommanager.get(frameid);
		listUsers.setListData(chatroom.getMemberIdsAsArray());
		//listUsers.repaint();
		while(chatroom.getMessages().size() > 0) 
			textareaMessages.append(chatroom.getMessages().remove(0)+"\n");
	}
	
	class SendAction extends KVNGroupwareClientAction{
		public SendAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			User user = loginmanager.getUser();
			Chatroom chatroom = chatroommanager.get(frameid);
			
			String[] attrNames = new String[]{"type", "action", "chatroomId", "userId"};
			String[] attrValues= new String[]{"chatroom", "send", chatroom.getId(), user.getId()};
			String text = textareaMessage.getText();
			Element[] content = new Element[0];
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
			
			textareaMessage.setText("");
			
		}
	}
	
	class LeaveAction extends KVNGroupwareClientAction{
		public LeaveAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			User user = loginmanager.getUser();
			Chatroom chatroom = chatroommanager.get(frameid);
			
			String[] attrNames = new String[]{"type", "action", "chatroomId", "userId"};
			String[] attrValues= new String[]{"chatroom", "leave", chatroom.getId(), user.getId()};
			String text = new String();
			Element[] content = new Element[0];
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);			
		}
	}

}
